package com.wh.process.ahelloworld;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;


/**
 * activiti流程引擎入门程序
 *
 * @author WeiHong
 * @date 2018/4/27
 */
public class HelloWorldTest {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程定义
     */
    @Test
    public void deploymentProcessDefinition() {
        //与流程定义和部署对象相关的Service
        Deployment deployment = processEngine.getRepositoryService()
                //创建一个部署对象
                .createDeployment()
                //添加部署名单
                .name("helloworld入门程序")
                //从classpath的资源中加载，一次只能加载一个文件
                .addClasspathResource("diagrams/MyProcess.bpmn")
                .addClasspathResource("diagrams/MyProcess.png")
                .deploy();
        System.out.println("部署对象ID = " + deployment.getId());
        System.out.println("部署对象名称 = " + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void startProcessInstance() {
        //key对应流程图中id
        String key = "helloworld";
        //与正在执行的流程实例和执行对象相关的Service
        ProcessInstance processInstance = processEngine.getRuntimeService()
                //使用流程定义的key启动流程实例，key对应helloworld.bpmn文件中id的属性值，使用key值启动，默认是按照最新版本的流程定义启动
                .startProcessInstanceByKey(key);
        System.out.println("流程实例ID = " + processInstance.getId());
        System.out.println("流程定义ID = " + processInstance.getProcessDefinitionId());
        //流程实例ID = 301
        //流程定义ID = helloworld:1:204
    }

    /**
     * 查找个人任务
     */
    @Test
    public void findMypersonalTasks() {
        String assignee = "张三";
        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee(assignee)
                .list();
        for (Task task : list) {
            System.out.println("任务ID task.getId() = " + task.getId());
            System.out.println("任务名称 task.getName() = " + task.getName());
            System.out.println("任务的创建时间 task.getCreateTime() = " + task.getCreateTime());
            System.out.println("任务的办理人 task.getAssignee() = " + task.getAssignee());
            System.out.println("流程实例ID task.getProcessInstanceId() = " + task.getProcessInstanceId());
            System.out.println("执行对象ID task.getExecutionId() = " + task.getExecutionId());
            System.out.println("流程定义ID task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
            System.out.println("--------------------------------------------------");
        }

    }

    /**
     * 完成个人任务
     */
    @Test
    public void completeMyPersonalTask(){
        String taskId = "2902";
        processEngine.getTaskService().complete(taskId);
        System.out.println("taskId = " + taskId);

    }

}
